package;

import nme.display.MovieClip;
import nme.display.Bitmap;
import nme.display.Shape;
import nme.text.TextFormat;
import nme.text.TextField;
import nme.text.TextFormatAlign;
import nme.text.Font;

/**
 * ...
 * @author jonathanmoriarty
 */
class TimeDisplay extends MovieClip, implements Display 
{

	private var _maxValue:Int;
	private var _minValue:Int;
	private var _currValue:Int;
	
	inline private static var MINUTES_X:Float = 0;
	
	inline private static var COLON_X:Float = 23;
	
	inline private static var SECONDS_X:Float = 32;
	
	private var _minutesDisplay:TextField;
	private var _colonDisplay:TextField;
	private var _secondsDisplay:TextField;
	
	public function new( l_x:Int, l_y:Int, l_minValue:Int, l_maxValue:Int, l_startValue:Int ) 
	{
		super();
		this.x = l_x;
		this.y = l_y;
		
		_minValue = l_minValue;
		_maxValue = l_maxValue;
		_currValue = l_startValue;
		
		//TODO: need monospaced font
		var l_font = Tools.loadFont("test_font");
		
		var l_minutesFormat = new TextFormat( l_font.fontName, 20, 0x000000 );
		l_minutesFormat.align = TextFormatAlign.RIGHT;
		
		var l_colonFormat = new TextFormat( l_font.fontName, 20, 0x000000 );
		
		var l_secondsFormat = new TextFormat( l_font.fontName, 20, 0x000000 );
		l_secondsFormat.align = TextFormatAlign.LEFT;
		
		_minutesDisplay = new TextField();
		_minutesDisplay.defaultTextFormat = l_minutesFormat;
		_minutesDisplay.selectable = false;
		_minutesDisplay.embedFonts = true;
		_minutesDisplay.width = 25;
		_minutesDisplay.height = 28;
		//_minutesDisplay.border = true;
		_minutesDisplay.x = MINUTES_X;
		addChild( _minutesDisplay );
		
		_colonDisplay = new TextField();
		_colonDisplay.defaultTextFormat = l_colonFormat;
		_colonDisplay.selectable = false;
		_colonDisplay.embedFonts = true;
		_colonDisplay.width = 9;
		_colonDisplay.height = 28;
		//_colonDisplay.border = true;
		_colonDisplay.x = COLON_X;
		_colonDisplay.text = ":";
		addChild( _colonDisplay );
		
		_secondsDisplay = new TextField();
		_secondsDisplay.defaultTextFormat = l_secondsFormat;
		_secondsDisplay.selectable = false;
		_secondsDisplay.embedFonts = true;
		_secondsDisplay.width = 35;
		_secondsDisplay.height = 28;
		//_secondsDisplay.border = true;
		_secondsDisplay.x = SECONDS_X;
		addChild( _secondsDisplay );
		
		var l_minutes:Int = Std.int(( _currValue / 1000 ) / 60);
		var l_seconds:Int = Std.int(( _currValue / 1000 ) % 60);
		
		_minutesDisplay.text = Std.string( l_minutes );
		
		if ( l_seconds < 10 )
		{
			_secondsDisplay.text = "0" + l_seconds;
		}
		else
		{
			_secondsDisplay.text = Std.string( l_seconds );
		}
	}		
	
	public function update( l_deltaTime:Int, l_currValue:Int ):Void
	{
		_currValue = l_currValue;
		if ( _currValue < _minValue )
		{
			_currValue = _minValue;
		}
		else if ( _currValue > _maxValue )
		{
			_currValue = _maxValue;
		}
		
		var l_minutes:Int = Std.int(( _currValue / 1000 ) / 60);
		var l_seconds:Int = Std.int(( _currValue / 1000 ) % 60);
		
		_minutesDisplay.text = Std.string( l_minutes );
		
		if ( l_seconds < 10 )
		{
			_secondsDisplay.text = "0" + l_seconds;
		}
		else
		{
			_secondsDisplay.text = Std.string( l_seconds );
		}
	}
}
